#!/usr/bin/env python
import addContactDialog, addGroupDialog, buddyWindow, chatWindow
import colourSelectionDialog, decryptDialog, emoteWindow
import fileSelectionDialog
import accountCreatorAssistant
from signals import fileSelectionDialogSignals
import tray
import gtk
import gettext

gettext.install('blendIT','locale');


class gui:
   def __init__(self,blend):
      self.blend = blend;
      gtk.rc_parse('blend.rc');

   def createTray(self):
      self.tray = tray.tray(self.blend);

   def initiateWindows(self):
      self.buddyWindow = buddyWindow.buddyWindow(self.blend);
      self.chatWindow = chatWindow.chatWindow(self.blend);
      self.buddyWindow.show();
      self.buddyWindow.changeMenuToLogOn();

   def initiateFirstTimeWindow(self):
      self.firstTimeWindow = accountCreatorAssistant.accountCreatorAssistant(self.blend);

   def destroyWindows(self):
      if self.buddyWindow != None:
         self.buddyWindow.destroy();

      if self.chatWindow != None:
         self.chatWindow.destroy();

   def requestPassword(self,label=None,recreateButton=False):
      dialog = gtk.Dialog(_("Enter Password"),self.buddyWindow,gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT);

      entry = gtk.Entry();
      entry.set_visibility(False);
      entry.set_max_length(10);
      if label == None:
         label = _("Please enter your password to log in to Mxit:");

      if recreateButton == True:
         dialog.add_button('_Recreate Account',gtk.RESPONSE_NO);
      
      dialog.add_buttons(gtk.STOCK_CANCEL,gtk.RESPONSE_REJECT,gtk.STOCK_APPLY, gtk.RESPONSE_ACCEPT);
      dialog.set_default_response(gtk.RESPONSE_ACCEPT);

      dialog.vbox.set_spacing(10);
      dialog.vbox.pack_start(gtk.Label(label));
      dialog.vbox.pack_start(entry);
      dialog.vbox.show_all();
      dialog.set_border_width(4);
      res = dialog.run();

      if res == gtk.RESPONSE_ACCEPT:
         if entry.get_text() == '':
            dialog.destroy();
            return False;
         else:
            txt = entry.get_text();
            dialog.destroy();
            return txt;
      elif res == gtk.RESPONSE_NO:
         dialog.destroy();
         return -1;
      else:
         dialog.destroy();         
         return False;

   def retryFailError(self):
      if self.buddyWindow.get_property('visible') == True:
         parent = self.buddyWindow;
      else:
         parent = None;
      self.createErrorMessage(parent,_("Could not to connect to MXit. Please check your internet connection and/or try again later."));

   def networkError(self):
      if self.buddyWindow.get_property('visible') == True:
         parent = self.buddyWindow;
      else:
         parent = None;
      self.createErrorMessage(parent,_("BlendIT could not connect to MXit due to network problems. Please try again in a few minutes."));                    

   def disconnectedMessage(self,reason):
      if self.buddyWindow.get_property('visible') == True:
         parent = self.buddyWindow;
      else:
         parent = None;
      self.createErrorMessage(parent,_("You have been disconnected from MXit for the following reason:\n%s") % reason);

   # Creates a generic error message dialog.
   def createErrorMessage(self,parent,message):
      messageDialog = gtk.MessageDialog(parent,gtk.DIALOG_MODAL,
                                        gtk.MESSAGE_ERROR,
                                        gtk.BUTTONS_OK,
                                        message);
      def sig(widget,res=None): widget.destroy();
      #messageDialog.connect('response',sig);
      messageDialog.connect('close',sig);
      r = messageDialog.run();
      messageDialog.hide();
      messageDialog.destroy();

   # Make a save dialog.
   def saveDialog(self,file):
      buddy = self.blend.conn.roster._files[file.id][1];
      
      # Now spawn the file save dialog
      fsigs = fileSelectionDialogSignals.signals(self.blend);
    
      dialog = fileSelectionDialog.fileSelectionDialog(self.blend,_("Save File As"),
               fsigs.on_save_file_ok,self.blend.gui.chatWindow,
               method=gtk.FILE_CHOOSER_ACTION_SAVE,params=(buddy,file));
      
      dialog.set_filter(dialog.list_filters()[3]);
      dialog.set_current_name(str(file.name));
      dialog.set_border_width(4);

      
      dialog.run();

      return False;
